package com.hnas.trade.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.common.service.dto.search.DTOCommoditOrderSearch;
import com.hnas.common.service.dto.search.DTOECardOrderSearch;
import com.hnas.common.service.dto.search.DTOOrderSearch;
import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.ecard.domain.CommoditOrderStatus;
import com.hnas.ecard.service.dto.DTOCommoditOrder;
import com.hnas.ecard.service.dto.DTOECardOrder;
import com.hnas.ecard.service.dto.DTOSaleCardOrder;
import com.hnas.trade.dao.OrderDao;
import com.hnas.trade.domain.OrderStatus;
import com.hnas.trade.domain.OrderType;
import com.hnas.trade.domain.PayStatus;
import com.hnas.trade.orm.Order;
import com.hnas.trade.service.dto.DTOOrder;
import com.hnas.trade.service.mapper.OrderMapper;

@Component("orderDao")
public class OrderDaoImpl extends BaseDaoImpl<Order> implements OrderDao{

	public OrderDaoImpl() {
		super(Order.class);
	}

	@Override
	public Order saveOrUpdate(DTOOrder dto) throws Exception {
		Order vo = null;
		try {
		
		if(dto != null){
			String id = dto.getId();
			if(StringUtil.isNotEmpty(id)){
				vo = this.find(id);
			}	
			if(vo == null){//新增
				vo = new Order();
				BeanUtils.copyProperties(dto, vo);
				this.persist(vo);
			}else{//修改
				BeanUtils.copyProperties(dto, vo, new String[]{"createTime","creator"});
				this.merge(vo);
			}
		}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}

	@Override
	public DTOOrder getByOrderNo(String order_no) {
		String hql = "from Order t where t.orderNo = ?1 ";
		Query query = this.entityManager.createQuery(hql,Order.class);
		query.setParameter(1, order_no);
		List<Order> list =  query.getResultList();
		if(null!=list&&list.size()>0){
			Order vo = list.get(0);
			return OrderMapper.getInstance().mapToValue(vo);
		}
		return null;
	}
	
	@Override
	public Order findByQRCode(String qrCode) {
		List<Order> list = this.findByQuery("from Order t where t.qrcodeNo = ?1", qrCode);
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void updateOrderStatus(String orderNo, String status) {
		String sql = "update m_base_wxxnk_order set order_status='"+status+"' ,update_time='"+DateUtil.dateTimeToString(new Date())+"' where order_no='"+orderNo+"'";
		Query query = this.getEntityManager().createNativeQuery(sql);
		query.executeUpdate();
		
	}
	
	@Override
	public DTOOrder getBySwiftNumber(String swiftNumber) {
		String hql = "from Order t where t.swiftNumber = ?1";
		Query query = this.entityManager.createQuery(hql,Order.class);
		query.setParameter(1, swiftNumber);
		Order order = (Order) query.getSingleResult();
		return OrderMapper.getInstance().mapToValue(order);
	}

	@Override
	public List<DTOOrder> getUnSuccessOrderList() {
		//查询状态是未付款或者付款中的且创建时间距离当前时间超过1分钟的订单
		String sql = "select * from m_base_wxxnk_order t where t.order_status = 3 or t.order_status = 0 and TIMESTAMPDIFF(SECOND,t.create_time,now()) >= 60";
		Query query = this.entityManager.createNativeQuery(sql, Order.class);
		List<Order> list = query.getResultList();
		List<DTOOrder> dtoList = new ArrayList<DTOOrder>();
		if(list != null && list.size() > 0){
			for(Order o : list){
				dtoList.add(OrderMapper.getInstance().mapToValue(o));
			}
		}
		return dtoList;
	}
	
	@Override
	public List<DTOOrder> getScanMrchPayingOrderList() {
		//查询状态是未付款或者付款中的且创建时间距离当前时间超过1分钟的订单
		String sql = "select * from m_base_wxxnk_order t where t.order_status = 3 and TIMESTAMPDIFF(SECOND,t.create_time,now()) >= 60";
		Query query = this.entityManager.createNativeQuery(sql, Order.class);
		List<Order> list = query.getResultList();
		List<DTOOrder> dtoList = new ArrayList<DTOOrder>();
		if(list != null && list.size() > 0){
			for(Order o : list){
				dtoList.add(OrderMapper.getInstance().mapToValue(o));
			}
		}
		return dtoList;
	}

	@Override
	public List<DTOOrder> getAccountOrderList() {
		String sql = "select * from m_base_wxxnk_order t where t.trade_type = 4 and t.order_status <> 1 order by t.create_time desc";
		Query query = this.entityManager.createNativeQuery(sql, Order.class);
		List<Order> list = query.getResultList();
		List<DTOOrder> dtoList = new ArrayList<DTOOrder>();
		if(list != null && list.size() > 0){
			for(Order o : list){
				dtoList.add(OrderMapper.getInstance().mapToValue(o));
			}
		}
		return dtoList;
	}

	@Override
	public DTOPagedList<DTOOrder> search(DTOOrderSearch criteria) {
		DTOPagedList<DTOOrder> pageList = new DTOPagedList<DTOOrder>();
		int pageNo = criteria.getPageNo();
		int pageSize = criteria.getPageSize();
		List<Object> paras = new ArrayList<Object>();
		StringBuffer baseSql = new StringBuffer(" from m_base_wxxnk_order t1 left join tbl_accinfo t2 on t1.acc_id = t2.id where 1=1 ");
		
		int j = 1;
		if(StringUtil.isNotEmpty(criteria.getOrderNo())){
			baseSql.append(" and t1.order_no = ?"+j++);
			paras.add(criteria.getOrderNo().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getPhoneNo())){
			baseSql.append(" and t3.phone_no = ?"+j++);
			paras.add(criteria.getPhoneNo().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderStatus())){
			baseSql.append(" and t1.order_status = ?"+j++);
			paras.add(criteria.getOrderStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderType())){
			baseSql.append(" and t1.order_type = ?"+j++);
			paras.add(criteria.getOrderType().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderStatus())){
			baseSql.append(" and t1.order_status = ?"+j++);
			paras.add(criteria.getOrderStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getPayStatus())){
			baseSql.append(" and t1.pay_status = ?"+j++);
			paras.add(criteria.getPayStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getSwiftNumber())){
			baseSql.append(" and t1.swift_number = ?"+j++);
			paras.add(criteria.getSwiftNumber().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getStartDate())){
			baseSql.append(" and t1.create_time >= ?"+j++);
			paras.add(criteria.getStartDate().trim()+" 00:00:00");
		}
		if(StringUtil.isNotEmpty(criteria.getEndDate())){
			baseSql.append(" and t1.create_time <= ?"+j++);
			paras.add(criteria.getEndDate().trim()+" 23:59:59");
		}
		
		baseSql.append(" order by t1.create_time desc");
		
		String sql2 = "select t1.order_no,t1.order_type,t1.order_status,t1.pay_status,t1.need_pay,t1.real_pay,t1.swift_number,t1.create_time,t1.trade_end_time,t2.phone_no "+baseSql.toString();
		String countSql = "select count(1) "+baseSql.toString();
		Query query = this.entityManager.createNativeQuery(sql2);
		Query countQuery = this.entityManager.createNativeQuery(countSql);
		for(int i= 1; i <= paras.size(); i++) {
			query.setParameter(i, paras.get(i-1));
			countQuery.setParameter(i, paras.get(i-1));
		}
		query.setFirstResult((pageNo-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Object[]> resultList = query.getResultList();
		int count = ((BigInteger)countQuery.getSingleResult()).intValue();
		pageList.setPageNo(pageNo);
		pageList.setPageSize(pageSize);
		pageList.setTotalCount(count);
		
		List<DTOOrder> list= new ArrayList<DTOOrder>();
		if(resultList != null && resultList.size() > 0) {
			for(Object[] obj : resultList) {
				DTOOrder dto = new DTOOrder();
				dto.setOrderNo(String.valueOf(obj[0]));
				dto.setOrderType(String.valueOf(obj[1]));
				dto.setOrderTypeName(OrderType.getOrderTypeName(dto.getOrderType()));
				dto.setOrderStatus(String.valueOf(obj[2]));
				if(StringUtil.isNotEmpty(dto.getOrderStatus())){
					dto.setOrderStatusName(OrderStatus.getOrderStatusString(dto.getOrderStatus()));
				}
				dto.setPayStatus(String.valueOf(obj[3]));
				if(StringUtil.isNotEmpty(dto.getPayStatus())){
					dto.setPayStatusName(PayStatus.getPayStatusString(dto.getPayStatus()));
				}
				dto.setNeedPay(((BigInteger)obj[4]).longValue());
				dto.setNeedPayDouble((double)dto.getNeedPay()/100);
				dto.setRealPay(((BigInteger)obj[5]).longValue());
				dto.setRealPayDouble((double)dto.getRealPay()/100);
				dto.setSwiftNumber(String.valueOf(obj[6]));
				dto.setCreateTime(String.valueOf(obj[7]));
				dto.setTradeEndTime(String.valueOf(obj[8]));
				dto.setMobilePhone(String.valueOf(obj[9]));
				list.add(dto);
			}
		}
		
		pageList.setList(list);
		return pageList;
	}
	
	public DTOPagedList<DTOOrder> search1(DTOOrderSearch criteria) {
		DTOPagedList<DTOOrder> pageList = new DTOPagedList<DTOOrder>();
		List<DTOOrder> list = new ArrayList<DTOOrder>();
		PageBean<Order> page = new PageBean<Order>();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		page.setTotalCount(criteria.getTotalCount());
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		if(StringUtil.isNotEmpty(criteria.getOrderNo())){
			sb.append(" and t.orderNo = ?"+j++);
			paras.add(criteria.getOrderNo().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getCardNo())){
			sb.append(" and t.cardNo = ?"+j++);
			paras.add(criteria.getCardNo().trim());
		}
		
		if(StringUtil.isNotEmpty(criteria.getOrderStatus())){
			sb.append(" and t.orderStatus = ?"+j++);
			paras.add(criteria.getOrderStatus().trim());
		}
		
		if(StringUtil.isNotEmpty(criteria.getOrderType())){
			sb.append(" and t.orderType = ?"+j++);
			paras.add(criteria.getOrderType().trim());
		}
		
		if(StringUtil.isNotEmpty(criteria.getOrderStatus())){
			sb.append(" and t.orderStatus = ?"+j++);
			paras.add(criteria.getOrderStatus().trim());
		}
		
		if(StringUtil.isNotEmpty(criteria.getPayStatus())){
			sb.append(" and t.payStatus = ?"+j++);
			paras.add(criteria.getPayStatus().trim());
		}
		
		if(StringUtil.isNotEmpty(criteria.getSwiftNumber())){
			sb.append(" and t.swiftNumber = ?"+j++);
			paras.add(criteria.getSwiftNumber().trim());
		}
		
		if(StringUtil.isNotEmpty(criteria.getStartDate())){
			sb.append(" and t.createTime >= ?"+j++);
			paras.add(criteria.getStartDate().trim()+" 00:00:00");
		}
		
		if(StringUtil.isNotEmpty(criteria.getEndDate())){
			sb.append(" and t.createTime <= ?"+j++);
			paras.add(criteria.getEndDate().trim()+" 23:59:59");
		}
		
		if(sb.length() > 0){
			sb.delete(0, 5);
			sb.insert(0, " from Order t  where ");
		}else{
			sb.append(" from Order t ");
		}
		sb.append(" order by t.createTime desc");
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) "+sb);
		this.queryByPage(page, paras.toArray());
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		for(Order vo : page.getList()){
			DTOOrder dto = new DTOOrder();
			dto = OrderMapper.getInstance().mapToValue(vo);
			list.add(dto);
		}
		pageList.setList(list);
		return pageList;
	}

	@Override
	public List<DTOSaleCardOrder> listOrderNoPay() {
		StringBuilder sb = queryOrderSql();
		sb.append(" from m_base_wxxnk_order t1 left join tbl_sale_card_order_detail t2 on t1.order_no = t2.order_no where (t1.order_status = 3 or t1.order_status = 0) and t1.order_type = ?1 and TIMESTAMPDIFF(SECOND,t1.create_time,now()) >= 60");
		Query query = this.entityManager.createNativeQuery(sb.toString());
		query.setParameter(1, OrderType.DZKGK.getType());
		List<Object[]> resultList = query.getResultList();
		if(resultList != null && resultList.size() > 0) {
			List<DTOSaleCardOrder> list = new ArrayList<DTOSaleCardOrder>();
			for(Object[] result : resultList) {
				DTOSaleCardOrder dto = assembleDTOSaleCardOrder(result);
				list.add(dto);
			}
			return list;
		}
		return null;
	}
	
	private StringBuilder queryOrderSql() {
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.id,")
		.append("t1.order_no,")
		.append("t1.order_type,")
		.append("t1.order_type_name,")
		.append("t1.acc_id,")
		.append("t1.acc_name,")
		.append("t1.order_detail,")
		.append("t1.order_status,")
		.append("t1.payment,")
		.append("t1.pay_status,")
		.append("t1.need_pay,")
		.append("t1.real_pay,")
		.append("t1.trade_type,")
		.append("t1.swift_number,")
		.append("t1.commidity_id,")
		.append("t1.commidity_type,")
		.append("t1.commidity_name,")
		.append("t1.card_no,")
		.append("t1.mrch_id,")
		.append("t1.mrch_name,")
		.append("t1.mrch_type,")
		.append("t1.psam_id,")
		.append("t1.sub_id,")
		.append("t1.trade_sno,")
		.append("t1.trade_end_time,")
		.append("t1.pay_end_time,")
		.append("t1.cancel_time,")
		.append("t1.create_time,")
		.append("t1.update_time,")
		.append("t1.is_deleted,")
		.append("t1.remark,")
		.append("t1.order_parent_type,")
		.append("t1.term_no,")
		.append("t2.sale_count,")
		.append("t2.denomination");
		return sb;
	}
	
	private DTOSaleCardOrder assembleDTOSaleCardOrder(Object[] result) {
		DTOSaleCardOrder dto = new DTOSaleCardOrder();
		dto.setId(String.valueOf(result[0]));
		dto.setOrderNo(String.valueOf(result[1]));
		dto.setOrderType(String.valueOf(result[2]));
		dto.setOrderTypeName(String.valueOf(result[3]));
		dto.setAccId(String.valueOf(result[4]));
		dto.setAccName(String.valueOf(result[5]));
		dto.setOrderDetail(String.valueOf(result[6]));
		dto.setOrderStatus(String.valueOf(result[7]));
		dto.setPayment(String.valueOf(result[8]));
		dto.setPayStatus(String.valueOf(result[9]));
		dto.setNeedPay(((BigInteger)result[10]).longValue());
		dto.setRealPay(((BigInteger)result[11]).longValue());
		dto.setTradeType(String.valueOf(result[12]));
		dto.setSwiftNumber(String.valueOf(result[13]));
		dto.setCommidityId(String.valueOf(result[14]));
		dto.setCommidityName(String.valueOf(result[15]));
		dto.setCommidityType(String.valueOf(result[16]));
		dto.setCardNo(String.valueOf(result[17]));
		dto.setMrchId(String.valueOf(result[18]));
		dto.setMrchName(String.valueOf(result[19]));
		dto.setMrchType(String.valueOf(result[20]));
		dto.setPsamId(String.valueOf(result[21]));
		dto.setSubId(String.valueOf(result[22]));
		dto.setTradeSno(String.valueOf(result[23]));
		dto.setTradeEndTime(String.valueOf(result[24]));
		dto.setPayEndTime(String.valueOf(result[25]));
		dto.setCancelTime(String.valueOf(result[26]));
		dto.setCreateTime(String.valueOf(result[27]));
		dto.setUpdateTime(String.valueOf(result[28]));
		dto.setIsDeleted(("0".equals(result[29]))?false:true);
		dto.setRemark(String.valueOf(result[30]));
		dto.setOrderParentType(String.valueOf(result[31]));
		dto.setTermNo(String.valueOf(result[32]));
		dto.setSaleCount((Integer)result[33]);
		dto.setDenomination(result[34]==null?0:((BigInteger)result[34]).longValue());
		return dto;
	}

	@Override
	public DTOPagedList<DTOCommoditOrder> searchCommoditOrder(DTOCommoditOrderSearch criteria) {
		DTOPagedList<DTOCommoditOrder> pageList = new DTOPagedList<DTOCommoditOrder>();
		List<DTOCommoditOrder> list = new ArrayList<DTOCommoditOrder>();
		int pageNo = criteria.getPageNo();
		int pageSize = criteria.getPageSize();
		List<Object> paras = new ArrayList<Object>();
		StringBuffer baseSql = new StringBuffer(" from m_base_wxxnk_order t1 left join tbl_commodit_order_detail t2 on t1.order_no = t2.order_no left join tbl_accinfo t3 on t3.id = t1.acc_id where 1=1 ");
		buildCommoditQueryCondition(criteria, paras, baseSql);
		baseSql.append(" order by t1.create_time desc");
		String listSql = "select t1.order_no,t1.order_type,t1.order_status,t1.pay_status,t1.need_pay,t1.real_pay,t1.swift_number,t1.create_time,t1.trade_end_time,t2.state,t3.phone_no "+baseSql;
		String countSql = "select count(1) "+baseSql;
		Query listQuery = this.entityManager.createNativeQuery(listSql);
		Query countQuery = this.entityManager.createNativeQuery(countSql);
		for(int i = 0;i < paras.size(); i++) {
			listQuery.setParameter(i+1, paras.get(i));
			countQuery.setParameter(i+1, paras.get(i));
		}
		listQuery.setFirstResult((pageNo-1)*pageSize);
		listQuery.setMaxResults(pageSize);
		int totalCount = ((BigInteger)countQuery.getSingleResult()).intValue();
		List<Object[]> resultList = listQuery.getResultList();
		buildCommoditOrderList(list, resultList);
		pageList.setTotalCount(totalCount);
		pageList.setPageNo(pageNo);
		pageList.setPageSize(pageSize);
		pageList.setList(list);
		return pageList;
	}

	private void buildCommoditOrderList(List<DTOCommoditOrder> list, List<Object[]> resultList) {
		if(resultList != null && resultList.size() > 0) {
			for(Object[] obj : resultList) {
				DTOCommoditOrder temp = new DTOCommoditOrder();
				temp.setOrderNo(String.valueOf(obj[0]));
				temp.setOrderType(String.valueOf(obj[1]));
				temp.setOrderTypeName(OrderType.getOrderTypeName(temp.getOrderType()));
				temp.setOrderStatus(String.valueOf(obj[2]));
				if(StringUtil.isNotEmpty(temp.getOrderStatus())){
					temp.setOrderStatusName(OrderStatus.getOrderStatusString(temp.getOrderStatus()));
				}
				temp.setPayStatus(String.valueOf(obj[3]));
				if(StringUtil.isNotEmpty(temp.getPayStatus())){
					temp.setPayStatusName(PayStatus.getPayStatusString(temp.getPayStatus()));
				}
				temp.setNeedPay(((BigInteger)obj[4]).longValue());
				temp.setNeedPayDouble((double)temp.getNeedPay()/100);
				temp.setRealPay(((BigInteger)obj[5]).longValue());
				temp.setRealPayDouble((double)temp.getRealPay()/100);
				temp.setSwiftNumber(String.valueOf(obj[6]));
				temp.setCreateTime(String.valueOf(obj[7]));
				temp.setTradeEndTime(String.valueOf(obj[8]));
				temp.setState(Integer.valueOf(String.valueOf(obj[9])).intValue());
				temp.setStateStr(CommoditOrderStatus.getDescByType(temp.getState()));
				temp.setMobilePhone(String.valueOf(obj[10]));
				list.add(temp);
			}
		}
	}

	private void buildCommoditQueryCondition(DTOCommoditOrderSearch criteria, List<Object> paras, StringBuffer baseSql) {
		int j = 1;
		if(StringUtil.isNotEmpty(criteria.getOrderNo())){
			baseSql.append(" and t1.order_no = ?"+j++);
			paras.add(criteria.getOrderNo().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getPhoneNo())){
			baseSql.append(" and t3.phone_no = ?"+j++);
			paras.add(criteria.getPhoneNo().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderStatus())){
			baseSql.append(" and t1.order_status = ?"+j++);
			paras.add(criteria.getOrderStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderType())){
			baseSql.append(" and t1.order_type = ?"+j++);
			paras.add(criteria.getOrderType().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderStatus())){
			baseSql.append(" and t1.order_status = ?"+j++);
			paras.add(criteria.getOrderStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getPayStatus())){
			baseSql.append(" and t1.pay_status = ?"+j++);
			paras.add(criteria.getPayStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getSwiftNumber())){
			baseSql.append(" and t1.swift_number = ?"+j++);
			paras.add(criteria.getSwiftNumber().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getState())){
			baseSql.append(" and t2.state = ?"+j++);
			paras.add(criteria.getState().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getStartDate())){
			baseSql.append(" and t1.create_time >= ?"+j++);
			paras.add(criteria.getStartDate().trim()+" 00:00:00");
		}
		if(StringUtil.isNotEmpty(criteria.getEndDate())){
			baseSql.append(" and t1.create_time <= ?"+j++);
			paras.add(criteria.getEndDate().trim()+" 23:59:59");
		}
	}

	@Override
	public DTOPagedList<DTOECardOrder> searchECardOrder(DTOECardOrderSearch criteria) {
		DTOPagedList<DTOECardOrder> pageList = new DTOPagedList<DTOECardOrder>();
		List<DTOECardOrder> list = new ArrayList<DTOECardOrder>();
		int pageNo = criteria.getPageNo();
		int pageSize = criteria.getPageSize();
		List<Object> paras = new ArrayList<Object>();
		StringBuffer baseSql = new StringBuffer(" from m_base_wxxnk_order t1 left join tbl_accinfo t3 on t3.id = t1.acc_id where 1=1 ");
		buildECardQueryCondition(criteria, paras, baseSql);
		baseSql.append(" order by t1.create_time desc");
		String listSql = "select t1.order_no,t1.order_type,t1.order_status,t1.pay_status,t1.need_pay,t1.real_pay,t1.swift_number,t1.create_time,t1.trade_end_time,t3.phone_no "+baseSql;
		String countSql = "select count(1) "+baseSql;
		Query listQuery = this.entityManager.createNativeQuery(listSql);
		Query countQuery = this.entityManager.createNativeQuery(countSql);
		for(int i = 0;i < paras.size(); i++) {
			listQuery.setParameter(i+1, paras.get(i));
			countQuery.setParameter(i+1, paras.get(i));
		}
		listQuery.setFirstResult((pageNo-1)*pageSize);
		listQuery.setMaxResults(pageSize);
		int totalCount = ((BigInteger)countQuery.getSingleResult()).intValue();
		List<Object[]> resultList = listQuery.getResultList();
		buildECardOrderList(list, resultList);
		pageList.setTotalCount(totalCount);
		pageList.setPageNo(pageNo);
		pageList.setPageSize(pageSize);
		pageList.setList(list);
		return pageList;
	}
	
	private void buildECardQueryCondition(DTOECardOrderSearch criteria, List<Object> paras, StringBuffer baseSql) {
		int j = 1;
		if(StringUtil.isNotEmpty(criteria.getOrderNo())){
			baseSql.append(" and t1.order_no = ?"+j++);
			paras.add(criteria.getOrderNo().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getPhoneNo())){
			baseSql.append(" and t3.phone_no = ?"+j++);
			paras.add(criteria.getPhoneNo().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderStatus())){
			baseSql.append(" and t1.order_status = ?"+j++);
			paras.add(criteria.getOrderStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderType())){
			baseSql.append(" and t1.order_type = ?"+j++);
			paras.add(criteria.getOrderType().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getOrderStatus())){
			baseSql.append(" and t1.order_status = ?"+j++);
			paras.add(criteria.getOrderStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getPayStatus())){
			baseSql.append(" and t1.pay_status = ?"+j++);
			paras.add(criteria.getPayStatus().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getSwiftNumber())){
			baseSql.append(" and t1.swift_number = ?"+j++);
			paras.add(criteria.getSwiftNumber().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getStartDate())){
			baseSql.append(" and t1.create_time >= ?"+j++);
			paras.add(criteria.getStartDate().trim()+" 00:00:00");
		}
		if(StringUtil.isNotEmpty(criteria.getEndDate())){
			baseSql.append(" and t1.create_time <= ?"+j++);
			paras.add(criteria.getEndDate().trim()+" 23:59:59");
		}
	}
	
	private void buildECardOrderList(List<DTOECardOrder> list, List<Object[]> resultList) {
		if(resultList != null && resultList.size() > 0) {
			for(Object[] obj : resultList) {
				DTOECardOrder temp = new DTOECardOrder();
				temp.setOrderNo(String.valueOf(obj[0]));
				temp.setOrderType(String.valueOf(obj[1]));
				temp.setOrderTypeName(OrderType.getOrderTypeName(temp.getOrderType()));
				temp.setOrderStatus(String.valueOf(obj[2]));
				if(StringUtil.isNotEmpty(temp.getOrderStatus())){
					temp.setOrderStatusName(OrderStatus.getOrderStatusString(temp.getOrderStatus()));
				}
				temp.setPayStatus(String.valueOf(obj[3]));
				if(StringUtil.isNotEmpty(temp.getPayStatus())){
					temp.setPayStatusName(PayStatus.getPayStatusString(temp.getPayStatus()));
				}
				temp.setNeedPay(((BigInteger)obj[4]).longValue());
				temp.setNeedPayDouble((double)temp.getNeedPay()/100);
				temp.setRealPay(((BigInteger)obj[5]).longValue());
				temp.setRealPayDouble((double)temp.getRealPay()/100);
				temp.setSwiftNumber(String.valueOf(obj[6]));
				temp.setCreateTime(String.valueOf(obj[7]));
				temp.setTradeEndTime(String.valueOf(obj[8]));
				temp.setMobilePhone(String.valueOf(obj[9]));
				list.add(temp);
			}
		}
	}
	
}
